<?php

namespace App\Http\Controllers;

use App\BO\PropertyAccessAndSetBO;
use App\Entities\DistributorEntity;
use App\Entities\QrcodeAdvertisingModel;
use App\ServicesNew\DistributorService;
use App\Utils\GenObjFromRequestUtil;
use App\VO\UserBalance\MetaInfo\BalanceInfo;
use App\VO\UserBalance\MetaInfo\TransactionRecordInfo;
use App\VO\UserBalance\TransactionSearchInfo;
use App\VO\UserBalance\UserBalanceIndexVO;
use App\VO\UserBalance\UserBalanceRecordVO;
use Carbon\Carbon;
use Illuminate\Http\Request;

class UserBalanceController extends BaseController
{
    /**
     * @api {get} /user/balance  用户账户余额
     * @apiGroup UserBalance
     *
     * @apiSuccessExample {json} success
     *     {"code":200,"message":"success","data":{"balance":{"balance":"0.00","freezeBalance":"0.00","preBalance":"0.00"},"user":{"all":1,"today":1}}}
     */
    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $userId = $this->getUserId();

        $distributorEntity = DistributorEntity::findOrCreateByUserId($userId);

        $balanceInfo = new BalanceInfo();
        if (!is_null($distributorEntity)) {
            PropertyAccessAndSetBO::sync($distributorEntity, $balanceInfo);
        }

        $userBalanceIndexVO = new UserBalanceIndexVO();
        $userBalanceIndexVO->setBalance($balanceInfo);

        //所有用户
        $all = QrcodeAdvertisingModel::query()->where('agentid', $userId)->count();
        //今日新增用户
        $today = QrcodeAdvertisingModel::query()->where('agentid', $userId)->where('create_at', '>=', Carbon::today()->getTimestamp())->count();
        $user = [
            'all' => $all,
            'today' => $today,
        ];

        $data = array_merge($userBalanceIndexVO->toArray(), ['user' => $user]);

        return $this->successJsonResult($data);
    }

    /**
     * @api {get} /user/balance/record  用户账户余额
     * @apiGroup UserBalance
     */
    /**
     * @param Request $request
     * @param DistributorService $distributorService
     * @return \Illuminate\Http\JsonResponse
     */
    public function record(Request $request, DistributorService $distributorService)
    {
        $transactionSearchInfo = $this->getSearchInfo($request);

        $transactionRecordEntities = $distributorService->getTransactionRecord($transactionSearchInfo);
        $total = $distributorService->count($transactionSearchInfo);

        $userBalanceRecordVO = new UserBalanceRecordVO();
        $userBalanceRecordVO->setSearch($transactionSearchInfo);
        $userBalanceRecordVO->setTotal($total);
        foreach ($transactionRecordEntities as $transactionRecordEntity) {
            /** * @var TransactionRecordInfo $record */
            $record = PropertyAccessAndSetBO::sync($transactionRecordEntity, new TransactionRecordInfo());
            $record->setMoney(abs($transactionRecordEntity->getMoney()));
            $userBalanceRecordVO->addRecord($record);
        }
        return $this->successJsonResult($userBalanceRecordVO);
    }

    /**
     * @param Request $request
     * @return TransactionSearchInfo
     */
    private function getSearchInfo(Request $request)
    {
        /** * @var TransactionSearchInfo $transactionSearchInfo */
        $transactionSearchInfo = GenObjFromRequestUtil::getObj(TransactionSearchInfo::class, $request);
        $transactionSearchInfo->setUserId($this->getUserId());
        return $transactionSearchInfo;
    }
}
